package com.auto.learning.tools;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.TextView;

import com.auto.learning.R;

/**
 * 加载进度对话框
 */
public class LoadingDialog extends GBSSDialog {
	TextView imageView;
	String text;
	Context context;

	public LoadingDialog(Context context,String text) {
		super(context, R.style.dialog);
		this.text = text;
		this.context = context;
		// super(context);
	}

	protected LoadingDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public LoadingDialog(Context context, int theme) {
		super(context, theme);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			Window window = this.getWindow();
			final Activity activity = (Activity)mContext ;
			WindowManager windowManager = activity.getWindowManager();
			Display display = windowManager.getDefaultDisplay();
			@SuppressWarnings("deprecation")
			int width = display.getWidth();
			@SuppressWarnings("deprecation")
			int height = display.getHeight();
			window.setLayout((int) (width * 0.85), (int) (height * 0.2));
		} catch (Exception e) {
		}

		this.setContentView(R.layout.loading_infinitus);
		imageView = (TextView) findViewById(R.id.imageside);
	}
	@Override
	public void show() {
		super.show();
		if (imageView !=null) {
			imageView.setText(text+"");
//			imageView.setTypeface(FontManager.typeface);
			Timer timer =new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					dismiss();
				}
			}, 1500);
		}
	}
	
	AnimationSet animationSet;
	public void startRotatAnimation(View view) {
		if (animationSet == null) {
			animationSet = new AnimationSet(true);
			LinearInterpolator lin = new LinearInterpolator();
			animationSet.setInterpolator(lin);
			animationSet.setFillAfter(true);
			RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_SELF, 0.5F,
					Animation.RELATIVE_TO_SELF, 0.5F);
			rotateAnimation.setDuration(1000);
			rotateAnimation.setRepeatCount(-1);
			animationSet.addAnimation(rotateAnimation);
		}
		view.startAnimation(animationSet);
	}
	@Override
	public void cancel() {
		super.cancel();
		if (imageView != null) {
//			imageView.clearAnimation();
		}
	}
	@Override
	public void dismiss() {
		super.dismiss();
		if (imageView != null) {
//			imageView.clearAnimation();
		}
	}
}
